package com.ld.security.server.admin.modules.gl.api;


import cn.hutool.core.util.ZipUtil;
import com.ld.security.common.core.exception.ApiException;
import com.ld.security.common.mybatis.base.vo.DataVo;
import com.ld.security.common.tomcat.anno.AjaxLogKey;
import com.ld.security.common.tomcat.base.api.SuperApi;
import com.ld.security.common.tomcat.util.RespUtil;
import com.ld.security.server.admin.modules.gl.entity.GlFileEntity;
import com.ld.security.server.admin.modules.gl.po.SpiceFilePo;
import com.ld.security.server.admin.modules.gl.service.IGlFileService;
import com.ld.security.server.admin.modules.gl.vo.UploadVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;


@RestController
@Api(tags="文件Api管理")
@RequestMapping("/api/glFile")
public class GlFileApi  extends SuperApi<GlFileEntity>{



        @Autowired
        IGlFileService iGlFileService;

        @Value("${upload.homePath}")
        private String basePath;

        @Value("${upload.webPath}")
        private String webPath;

        public final static String pub="nowbook-picture";

        public final static  String pri="nowbook-info";

        public final static  String  pubUrl = "https://nowbook-picture.oss-cn-hongkong.aliyuncs.com/";

        public final static  String  priUrl = "https://nowbook-info.oss-cn-hongkong.aliyuncs.com/";

        @AjaxLogKey

        @ApiOperation("vue资源上传")
        @RequestMapping(value="/vue",method = RequestMethod.POST)
        public DataVo vue(MultipartFile multipartFile) throws IOException {
                String name = multipartFile.getOriginalFilename();
                if(!name.endsWith(".zip")){
                        ApiException.throw400("必须为zip文件");
                }

                String id =  DigestUtils.md5DigestAsHex(multipartFile.getBytes());

                GlFileEntity fileEntity = iGlFileService.getById(id);
                String[] args = name.split("\\.");
                String fileName=args[1]+ "/"+ id+"."+args[1];

                if(fileEntity == null){

                        File file = new File(this.basePath+fileName);
                        file.getParentFile().mkdir();
                        multipartFile.transferTo(file);
                        ZipUtil.unzip(file,Charset.forName("GBK"));

                        fileEntity =  this.newInstance();
                        fileEntity.setId(id);
                        fileEntity.setFileName(name);
                        fileEntity.setFileType(".vue");
                        fileEntity.setFilePath(file.getPath());
                        iGlFileService.save(fileEntity);
                }

                String url=webPath+args[1]+ "/"+ id+"/index.html";

                UploadVo vo = new UploadVo(fileEntity.getFileName(),url);


                return DataVo.of(vo);
        }


        @AjaxLogKey

        @ApiOperation("用户文件上传")
        @RequestMapping(value="/pub",method = RequestMethod.POST)
        public DataVo pub( SpiceFilePo po) throws IOException {
                String name = null ;

                         name = iGlFileService.spliceFile(po);

                UploadVo vo =new UploadVo(po.getFileId(),name);


                return DataVo.of(vo);
        }


        @AjaxLogKey
        @ApiOperation("文件下载")
        @RequestMapping(value="/downloadFile",method = RequestMethod.GET)
        @SneakyThrows
        public void downloadFile(String id){
                GlFileEntity fileEntity =  iGlFileService.getById(id);
                File file = new File(fileEntity.getFilePath());
                RespUtil.downloadFile(file);
        }


        public DataVo uploadFile( MultipartFile multipartFile) throws IOException {

                String id =  DigestUtils.md5DigestAsHex(multipartFile.getBytes());
                GlFileEntity fileEntity = iGlFileService.getById(id);
                String name = multipartFile.getOriginalFilename();
                String[] args = name.split("\\.");
                String fileName=args[1]+"/"+ id+"."+args[1];

                try{
                        File p =   new File(basePath+args[1]);
                        if(!p.exists()){
                                p.mkdirs();
                        }

                }catch (Exception e){
                        e.printStackTrace();
                }


                if(fileEntity == null){


                        File file = new File(this.basePath+ fileName);

                        multipartFile.transferTo(file);


                        fileEntity =  this.newInstance();
                        fileEntity.setId(id);
                        fileEntity.setFileName(name);
                        fileEntity.setFileType( args[1]);
                        fileEntity.setFilePath(file.getPath());
                        iGlFileService.save(fileEntity);
                }

                String url=webPath+fileName;

                UploadVo vo = new UploadVo(fileEntity.getFileName(),url);



                return DataVo.of(vo);
        }

        /**
         * 获得文件后缀
         * @param name
         * @return
         */
        public static String getSuffix(String name){
                return name.substring(name.lastIndexOf("."),name.length());
        }

}
